package com.geese.module.ceo.controller;

import java.util.List;
import java.util.Map;

import com.geese.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geese.common.BaseController;
import com.geese.common.constants.Constants;
import com.geese.common.entity.Courier;
import com.geese.common.util.BeanUtil;
import com.geese.common.util.TextValidator;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.module.ceo.dto.WaiterDto;
import com.geese.module.ceo.service.CourierService;
import com.geese.module.ceo.vo.WaiterQuery;
import com.geese.module.ceo.vo.WaiterVO;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author YeFangYi 
 */
@Controller
@RequestMapping(value = "/ceo")
@Scope("prototype")
public class WaiterAdminController extends BaseController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private CourierService courierService;

	/**
     * 修改小二
     */
    @PostMapping("/waiter/update")
    @ResponseBody
    public String waiterUpdate(@RequestBody WaiterVO waiterVO, @RequestParam String token){
    	if(!StringUtils.isNumeric(waiterVO.getId())) {
    		throw new BusinessException(120040);
    	}
//    	if(StringUtils.isEmpty(waiterVO.getCode())) {
//    		throw new RuntimeException("学号不能为空");
//    	}
    	if(!TextValidator.isMobileExact(waiterVO.getMobile())) {
    		throw new BusinessException(120012);
    	}
    	if(StringUtils.isEmpty(waiterVO.getName())) {
    		throw new BusinessException(120013);
    	}
    	if(!StringUtils.isNumeric(waiterVO.getCampusId())) {
    		throw new BusinessException(120039);
    	}
    	if(!StringUtils.isNumeric(waiterVO.getBuildingId())) {
    		throw new BusinessException(120038);
    	}
    	if(!TextValidator.isUrl(waiterVO.getStudentCard())) {
    		throw new BusinessException(120041);
    	}
    	userService.checkUser(token);
    	
    	Courier courier = BeanUtil.map(waiterVO, Courier.class);
    	courierService.updateWaiter(courier);
        return response();
    }
    
    /**
     * 删除小二
     */
    @PostMapping("/waiter/remove")
    @ResponseBody
    public String waiterUpdate(@RequestParam String id, @RequestParam String token){
    	if(!StringUtils.isNumeric(id)) {
    		throw new BusinessException(120010);
    	}
    	
    	userService.checkUser(token);
    	courierService.remove(Integer.valueOf(id));
        return response();
    }
    
    /**
     * 审核小二
     */
    @PostMapping("/waiter/check")
    @ResponseBody
    public String waiterCheck(@RequestParam String id, @RequestParam String token){
    	if(!StringUtils.isNumeric(id)) {
    		throw new BusinessException(120010);
    	}
    	
    	SecurityUser user = userService.getLoginUser(token);
    	Courier courier = courierService.findByUserId(user.getUserId());
    	courierService.check(Integer.valueOf(id), courier.getSchoolId());
        return response();
    }
    
    /**
     * 批量审核小二
     */
    @PostMapping("/waiter/batchCheck")
    @ResponseBody
    public String waiterbatchCheck(@RequestParam String ids, @RequestParam String token){
    	List<String> tempIds = Splitter.on(",").trimResults().splitToList(ids);
    	if(CollectionUtils.isEmpty(tempIds)) {
    		throw new BusinessException(120016);
    	}
    	List<Integer> waiterIds = Lists.transform(checkIds(tempIds), new Function<String, Integer>() {
			@Override
			public Integer apply(String tempId) {
				return Integer.valueOf(tempId);
			}
		});
    	
    	SecurityUser user = userService.getLoginUser(token);
    	Courier courier = courierService.findByUserId(user.getUserId());
    	courierService.batchCheck(waiterIds, courier.getSchoolId());
        return response();
    }
    
    /**
     * 查询小二列表
     */
    @GetMapping("/waiter/listInPage")
    @ResponseBody
    public String waiterList(WaiterQuery query) {
		if (StringUtils.isBlank(query.getSchoolId()) && !StringUtils.isNumeric(query.getSchoolId())) {
			throw new BusinessException(120015);
		}
    	if(StringUtils.isNotEmpty(query.getCampusId()) && !StringUtils.isNumeric(query.getCampusId())) {
    		throw new BusinessException(120039);
    	}
    	if(StringUtils.isNotEmpty(query.getBuildingId()) && !StringUtils.isNumeric(query.getBuildingId())) {
    		throw new BusinessException(120038);
    	}
    	userService.checkUser(query.getToken());
    	
    	Page page = getPageInfo(query);
    	Paginator<WaiterDto> pageList = courierService.listAllByQuery(query, page);
    	Map<String, Object> result = Maps.newHashMap();
    	result.put(Constants.PAGE_INFO, pageList.getPageResult());
    	result.put("courierList", pageList.getResults());
        return response(result);
    }
}
